package com.cms.recursoshumanos.facade;

import java.io.UnsupportedEncodingException;

import net.rim.device.api.ui.UiApplication;

import com.cms.recursoshumanos.constant.Constant;
import com.cms.recursoshumanos.engine.ScreenEngine;
import com.cms.recursoshumanos.handler.BaseHandler;
import com.cms.recursoshumanos.handler.JParserHandler;
import com.cms.recursoshumanos.handler.WServiceCall;
import com.cms.recursoshumanos.json.object.JsonObjectGetFamiliar;
import com.cms.recursoshumanos.popup.ShowDialogAlert;
import com.cms.recursoshumanos.transport.BBRequest;
import com.cms.recursoshumanos.transport.BBRequestQueue;
import com.cms.recursoshumanos.transport.HttpRequest;
import com.cms.recursoshumanos.utilities.Tool;

public class ListaFamiliarScreenFacade extends BaseFacade implements BBRequest.Listener
{

	private String strUsername;
	private String strPass;
	private int intDecision;
	
	private HttpRequest req;
	
	ShowDialogAlert alerta;
	
	public ListaFamiliarScreenFacade(String user,
										String password,
										int decision)
	{
		this.strUsername = user;
		this.strPass = password;
		this.intDecision = decision;
		
	}
	
	public void getListaFamiliar()
	{
		enviarSolicitud( WServiceCall.getInstance().getListaFamiliar(strUsername,strPass));
	}
	
	public void enviarSolicitud(String envelope_soap) 
	{
		try 
		{
			req = new HttpRequest();
			req.setRequestURL(Constant.URL_WEBSERVICE);	
			req.setPayload(envelope_soap, "UTF-8");
			req.setListener(this);
	        BBRequestQueue.getInstance().addRequest(req);
		} 
		catch (UnsupportedEncodingException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		catch (Exception e)
		{
			e.printStackTrace();
		}
		
        
	}
	
	public void requestStarted(BBRequest request) 
	{
		synchronized (UiApplication.getEventLock()) 
		{
			ScreenEngine.getInstance().showLoadingPopup("Cargando");
		}
		
	}
	
	public void requestCancelled(BBRequest request) 
	{
		ScreenEngine.getInstance().hideLoadingPopup();
		alerta = new ShowDialogAlert("Se cayo la conexion inesperadamente, intente nuevamente");
		alerta.run();
	}

	public void requestFailed(BBRequest request)
	{
		ScreenEngine.getInstance().hideLoadingPopup();
		alerta = new ShowDialogAlert("No se pudo conectar al servidor, intete nuevamente");
		alerta.run();
	}


	public void requestSucceeded(BBRequest request) 
	{
		String result = JParserHandler.getInstance().procesarXML(request);
		ScreenEngine.getInstance().hideLoadingPopup();
		this.setValores(result);

	}
	
	public void setValores(String strResult)
	{
		if (strResult.equals(null))
		{
			ShowDialogAlert alerta = new ShowDialogAlert("No se pudo obtener informacion del servidor, chequee su conexion");
			alerta.run();
		}
		else
		{
		
			JsonObjectGetFamiliar.getInstance().fromJSON(strResult);
			
			if (JsonObjectGetFamiliar.getInstance().getResult().equals(Constant.CODE_OK))
			{	
				synchronized (UiApplication.getEventLock()) 
				{
					if (intDecision == 1)
						BaseHandler.getInstance().goListaFamiliar();
					else if (intDecision == 2)
						BaseHandler.getInstance().goListaBeneficiario();
				}
			}
			else
			{
				Tool.getInstance().showMessageError(JsonObjectGetFamiliar.getInstance().getResult());
			}
			
			/*if (JsonObjectGetFamiliar.getInstance().getResult().equals(Constant.CODE_EMPTY))
			{
				alerta = new ShowDialogAlert("No se obtuvo informacion del servidor");
				alerta.run();
			}
			else if (JsonObjectGetFamiliar.getInstance().getResult().equals(Constant.CODE_NOT_OK))
			{
				alerta = new ShowDialogAlert("Error en el servidor intente nuevamente");
				alerta.run();
			}
			else if (JsonObjectGetFamiliar.getInstance().getResult().equals(Constant.CODE_OK))
			{	
				synchronized (UiApplication.getEventLock()) 
				{
					if (intDecision == 1)
						handler.goListaFamiliar();
					else if (intDecision == 2)
						handler.goListaBeneficiario();
				}
			}*/
		}
	}
	
	

}
